/**
 * 添加或者修改页面
 */
var StockInfoDlg = {
    data: {
        id: "",
        productId: "",
        supplierId: "",
        shopId: "",
        warehouseId: "",
        shelfId: "",
        saleUsableCount: "",
        saleFreezeCount: "",
        saleUsedCount: "",
        warehouseUsableCount: "",
        warehouseFreezeCount: "",
        warehouseUsedCount: "",
        version: "",
        createTime: "",
        updateTime: ""
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/stock/addItem", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //商品
    $.ajax({
        url: '/baseCommonSelect/getProductList',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            if (data !== null) {
                $("#productId").empty();
                $("#productId").append(new Option());
                $.each(data, function (index, item) {
                    $('#productId').append(new Option(item.value, item.id));
                });
            } else {
                $("#productId").append(new Option("暂无数据", ""));
            }
            //重新渲染
            form.render("select");
        }
    });

    //供应商
    $.ajax({
        url: '/baseCommonSelect/getFactoryNumList',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            if (data !== null) {
                $("#supplierId").empty();
                $("#supplierId").append(new Option());
                $.each(data, function (index, item) {
                    $('#supplierId').append(new Option(item.value, item.id));
                });
            } else {
                $("#supplierId").append(new Option("暂无数据", ""));
            }
            //重新渲染
            form.render("select");
        }
    });

    //分店
    $.ajax({
        url: '/baseCommonSelect/getShopList',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            if (data !== null) {
                $("#shopId").empty();
                $("#shopId").append(new Option());
                $.each(data, function (index, item) {
                    $('#shopId').append(new Option(item.value, item.id));
                });
            } else {
                $("#shopId").append(new Option("暂无数据", ""));
            }
            //重新渲染
            form.render("select");
        }
    });

    //仓库
    $.ajax({
        url: '/baseCommonSelect/initWarehouseInfos',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            if (data !== null) {
                $("#warehouseId").empty();
                $("#warehouseId").append(new Option());
                $.each(data, function (index, item) {
                    $('#warehouseId').append(new Option(item.value, item.id));
                });
            } else {
                $("#warehouseId").append(new Option("暂无数据", ""));
            }
            //重新渲染
            form.render("select");
        }
    });

    //监听仓库的下拉框事件，设置货架下拉框
    form.on('select(warehouseIdSelect)', function (data) {
        var warehouseId = data.value;
        $.ajax({
            url: '/baseCommonSelect/initShelfSelectOptions',
            dataType: 'json',
            data: {warehouseId: warehouseId},
            type: 'post',
            success: function (data) {
                if (data !== null) {
                    $("#shelfId").empty();
                    $("#shelfId").append(new Option());
                    $.each(data, function (index, item) {
                        $('#shelfId').append(new Option(item.value, item.id));
                    });
                } else {
                    $("#shelfId").append(new Option("暂无数据", ""));
                }
                //重新渲染
                form.render("select");
            }
        });

    });


});